package com.kma.ncpractice2013.servlets;

import com.kma.ncpractice2013.dao.UserDAO;
import com.kma.ncpractice2013.facade.MailFacade;
import com.kma.ncpractice2013.facade.MailFacadeImpl;
import com.kma.ncpractice2013.model.User;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * User: Viktor
 * Date: 11/17/13
 */


public class UserProfileServlet extends HttpServlet
{
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		UserDAO userDAO = new UserDAO();
		User user = userDAO.getByLogin(((User)request.getSession(false).getAttribute("user")).getLogin());
		String field = request.getParameter("attr");
		String value = request.getParameter("val");

		if (field.equals("birthdate"))
		{
			Date d;
			try
			{
				d = Date.valueOf(value);
			}
			catch(Exception e)
			{
				respond("error", "Specify date", response);

				return;
			}
			user.setBirthdate(d);
			userDAO.update(user);


		}
		else if (field.equals("name"))
		{
			if (value.length()<3)
			{
				respond("error", "Name too short, must be longer than 2 characters", response);

				return;
			}
			user.setName(value);
			userDAO.update(user);

		}
		else if (field.equals("surname"))
		{
			if (value.length()<3)
			{
				respond("error", "Surname too short, must be longer than 2 characters", response);

				return;
			}

			user.setSurname(value);
			userDAO.update(user);

		}
		else if (field.equals("phone"))
		{
			if (!value.matches("[0-9]*$"))
			{
				respond("error", "Phone number should contain only numbers", response);

				return;
			}
			user.setPhone(value);
			userDAO.update(user);

		}
		else if (field.equals("email"))
		{
			if(!value.matches(".*[@].+"))
			{
				respond("error", "Please enter valid e-mail", response);

				return;
			}
			request.getSession(false).setAttribute("changeMail", value);
			user.setRegistrationToken(UUID.randomUUID().toString());

			userDAO.update(user);

			MailFacade mail=new MailFacadeImpl();
			String subject = "Reservation System e-mail change confirmation";
			String letter = "Click on the following link to confirm change.\n" +
					                MailFacadeImpl.home + "changemail.jsp?token=" + user.getRegistrationToken();
			try
			{
				mail.sendLetter(value, subject, letter);

			}
			catch (Exception e)
			{
				response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/error.jsp"));
				return;
			}
			request.getSession(false).setAttribute("user", user);
			response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/main_page.jsp"));
		}
		else response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/error.jsp"));

		request.getSession(false).setAttribute("user", user);
		respond("ok", value, response);

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

	}

	private void respond(String status, String json_response, HttpServletResponse response) throws IOException
	{
		HashMap<String, String> resp = new HashMap<String, String>();
		resp.put("status", status);
		resp.put("response", json_response);
		response.getWriter().print(JSONObject.valueToString(resp));
	}
}
